package com.yzy.gateway.center.infrastructure.repository;

import com.yzy.gateway.center.domain.manage.model.vo.GatewayServerDetailVO;
import com.yzy.gateway.center.domain.manage.model.vo.GatewayServerVO;
import com.yzy.gateway.center.domain.manage.repository.IConfigManageRepository;
import com.yzy.gateway.center.infrastructure.dao.IGatewayServerDao;
import com.yzy.gateway.center.infrastructure.dao.IGatewayServerDetailDao;
import com.yzy.gateway.center.infrastructure.po.GatewayServer;
import com.yzy.gateway.center.infrastructure.po.GatewayServerDetail;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @version: java version 1.8
 * @Author: yzy
 * @description: 仓储层, 对Dao层进行数据封装
 * @date: 2023-10-25 15:51
 */
@Component
public class ConfigManageRepository implements IConfigManageRepository {
    @Resource
    private IGatewayServerDao gatewayServerDao;
    @Resource
    private IGatewayServerDetailDao gatewayServerDetailDao;

    @Override
    public List<GatewayServerVO> queryGatewayServerList() {
        List<GatewayServer> gatewayServerList = gatewayServerDao.queryGatewayServerList();
        List<GatewayServerVO> gatewayServerVOList = new ArrayList<>(gatewayServerList.size());
        for (GatewayServer gatewayServer : gatewayServerList) {
            GatewayServerVO gatewayServerVO = new GatewayServerVO(gatewayServer.getGroupId(), gatewayServer.getGroupName());
            gatewayServerVOList.add(gatewayServerVO);
        }
        return gatewayServerVOList;
    }

    @Override
    public GatewayServerDetailVO queryGatewayServerDetail(String gatewayId, String gatewayAddress) {
        GatewayServerDetail req = new GatewayServerDetail();
        req.setGatewayId(gatewayId);
        req.setGatewayAddress(gatewayAddress);
        GatewayServerDetail gatewayServerDetail = gatewayServerDetailDao.queryGatewayServerDetail(req);
        if (null == gatewayServerDetail) return null;
        // 可以按照 IDEA 插件 vo2dto 方便转换
        GatewayServerDetailVO gatewayServerDetailVO = new GatewayServerDetailVO();
        gatewayServerDetailVO.setGatewayId(gatewayServerDetail.getGatewayId());
        gatewayServerDetailVO.setGatewayName(gatewayServerDetail.getGatewayName());
        gatewayServerDetailVO.setGatewayAddress(gatewayServerDetail.getGatewayAddress());
        gatewayServerDetailVO.setStatus(gatewayServerDetail.getStatus());
        return gatewayServerDetailVO;

    }

    @Override
    public boolean updateGateStatus(String gatewayId, String gatewayAddress, Integer available) {
        GatewayServerDetail gatewayServerDetail = new GatewayServerDetail();
        gatewayServerDetail.setGatewayId(gatewayId);
        gatewayServerDetail.setGatewayAddress(gatewayAddress);
        gatewayServerDetail.setStatus(available);
        return gatewayServerDetailDao.updateGatewayStatus(gatewayServerDetail);

    }

    @Override
    public boolean registerGatewayServerNode(String groupId, String gatewayId, String gatewayName, String gatewayAddress, Integer available) {
        GatewayServerDetail gatewayServerDetail = new GatewayServerDetail();
        gatewayServerDetail.setGroupId(groupId);
        gatewayServerDetail.setGatewayId(gatewayId);
        gatewayServerDetail.setGatewayName(gatewayName);
        gatewayServerDetail.setGatewayAddress(gatewayAddress);
        gatewayServerDetail.setStatus(available);
        gatewayServerDetailDao.insert(gatewayServerDetail);
        return true;

    }
}
